//
//  HttpRequest.cpp
//  EZC_Library
//
//  Created by ellzu on 14-9-23.
//  Copyright (c) 2014年 ellzu. All rights reserved.
//

#include "HttpRequest.h"
#include <assert.h>

using namespace EZHttp;

HttpRequest::HttpRequest(HttpRequestPlatformInterface *hrpi)
{
    _hrpi = 0;
    assert(hrpi);
    setHRPI(hrpi);
    
    setMethod("GET");
    
    _response = 0;
    _delegate = 0;
    setProgressCount(100);
    
}
HttpRequest::~HttpRequest()
{
    setHRPI(0);
    setResponse(0);
}

void HttpRequest::setUrl(const std::string &url)
{
    _url = url;
//    _hrpi->setUrl(url);
}
std::string HttpRequest::url()
{
    return _url;
}

std::string HttpRequest::method()
{
    return _method;
}
void HttpRequest::setMethod(const std::string &method)
{
    _method = method;
//    _hrpi->setMethod(method);
}

void HttpRequest::setHRPI(HttpRequestPlatformInterface *hrpi)
{
    if (hrpi!=0) {
        hrpi->Retain();
//        hrpi->_ownRequest = this;
        hrpi->setOwnRequest(this);
    }
    if (_hrpi!=0) {
        _hrpi->Release();
//        _hrpi->_ownRequest = 0;
        _hrpi->setOwnRequest(0);
    }
    _hrpi = hrpi;
}
HttpRequestPlatformInterface* HttpRequest::HRPI()
{
    return _hrpi;
}

void HttpRequest::setRequestValue(const std::string &key,const std::string &value)
{
    _requestValues[key] = value;
//    _hrpi->setRequestValue(key, value);
}
std::string HttpRequest::requestValue(const std::string &key)
{
    std::map<std::string,std::string>::iterator it = _requestValues.find(key);
    if (it == _requestValues.end()) {
        return "";
    }else{
        return it->second;
    }
}

void HttpRequest::setHeader(const std::string &key,const std::string &value)
{
    _headers[key] = value;
//    _hrpi->setHeader(key, value);
}
std::string HttpRequest::header(const std::string &key)
{
    std::map<std::string,std::string>::iterator it = _headers.find(key);
    if (it == _headers.end()) {
        return "";
    }else{
        return it->second;
    }
}

void HttpRequest::startSync()
{
    HttpResponse *response = new HttpResponse();
    setResponse(response);
    response->Release();
    
    if (_delegate) {
        _delegate->onHttpRequestStart(this);
    }
    _hrpi->startSync();
    
    if (_delegate) {
        _delegate->onHttpRequestFinish(this);
    }
    
}
void HttpRequest::startAsync()
{
    
}

void HttpRequest::setDownLoadPath(const std::string &path)
{
    _downLoadPath = path;
}
std::string HttpRequest::downLoadPath()
{
    return _downLoadPath;
}

void HttpRequest::setTempPath(const std::string &path)
{
    _tempPath = path;
}
std::string HttpRequest::tempPath()
{
    return _tempPath;
}

void HttpRequest::setResponse(HttpResponse *response)
{
    if (response) {
        response->Retain();
        response->setRequest(this);
    }
    if (_response) {
        _response->Release();
        _response->setRequest(0);
    }
    _response = response;
}
HttpResponse* HttpRequest::response()
{
    return _response;
}

void HttpRequest::setDelegate(HttpRequestDelegate *delegate)
{
    _delegate = delegate;
}
HttpRequestDelegate* HttpRequest::delegate()
{
    return _delegate;
}

void HttpRequest::setProgressCount(int count)
{
    _progressCount = count;
}
int HttpRequest::progressCount()
{
    return _progressCount;
}

